package math2;

import inf.math.LinAlg;

/**
 * TODO
 * 
 * @author hbui
 * 
 */
public class SumFunctionRnToR extends FunctionRnToR {

	private double alpha;
	private double beta;
	private FunctionRnToR u;
	private FunctionRnToR v;

	public SumFunctionRnToR(double alpha, FunctionRnToR u, double beta,
			FunctionRnToR v) {
		this.alpha = alpha;
		this.beta = beta;
		this.u = u;
		this.v = v;
	}

	@Override
	public double[] gradientAt(double... x) {
		double[] du = u.gradientAt(x);
		double[] dv = v.gradientAt(x);
		double[] g = dv.clone();
		LinAlg.add(x.length, alpha, du, beta, g);
		return g;
	}

	@Override
	public double valueAt(double... x) {
		return alpha * u.valueAt(x) + beta * v.valueAt(x);
	}

	@Override
	public int getP() {
		if (this.u.getP() == -1 || this.v.getP() == -1) {
			return -1;
		} else {
			return Math.max(this.u.getP(), this.v.getP());
		}
	}
}
